package com.deepseek.controller;

import com.deepseek.model.dto.ChatRequest;
import com.deepseek.model.dto.ChatResponse;
import com.deepseek.utils.StreamHttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/chat")
public class ChatController {
    private static final Logger logger = LoggerFactory.getLogger(ChatController.class);

    @PostMapping
    public ChatResponse chat(@RequestBody ChatRequest request) {
        // 打印接收到的请求参数
        logger.debug("收到用户请求：{}", request.getUserId());
        logger.info("问题内容：{}", request.getMessage());

        try {
            // 构建请求体
            String jsonInput = String.format(
                "{\"messages\":[{\"id\": \"%s\", \"role\": \"user\", \"content\": \"%s\"}], " +
                "\"temperature\":0.7," +
                "\"top_p\":0.9," +
                "\"stream\":\"True\"}",
                request.getUserId(),
                request.getMessage()
            );

            // 调用 StreamHttpRequest 获取响应
            StreamHttpRequest.GetAnswerOut(jsonInput);
            
            // 返回成功响应
            return ChatResponse.success("请求已处理");
        } catch (Exception e) {
            logger.error("处理请求时发生错误", e);
            return ChatResponse.error("处理请求时发生错误: " + e.getMessage());
        }
    }
}